From 82fc887ce383c976d3694c44391eb0b496659293 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 23 Jul 2014 11:44:15 -0700 Subject: [PATCH] Fix integration tests with same names as libs Use extra-filename mixins to ensure that these names never clash. --- src/cargo/core/manifest.rs | 5 +++-- src/cargo/core/package_id.rs | 8 ++++++++ src/cargo/util/toml.rs | 7 ++++++- tests/test_cargo_test.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 8d148e047..301831fc0 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -351,13 +351,14 @@ impl Target { } } - pub fn test_target(name: &str, src_path: &Path, profile: &Profile) -> Target { + pub fn test_target(name: &str, src_path: &Path, + profile: &Profile, metadata: Metadata) -> Target { Target { kind: BinTarget, name: name.to_string(), src_path: src_path.clone(), profile: profile.clone(), - metadata: None + metadata: Some(metadata), } } diff --git a/src/cargo/core/package_id.rs b/src/cargo/core/package_id.rs index 51ce421d2..02bf6fc29 100644 --- a/src/cargo/core/package_id.rs +++ b/src/cargo/core/package_id.rs @@ -126,6 +126,14 @@ impl PackageId { } } +impl Metadata { + pub fn mix(&mut self, t: &T) { + let new_metadata = short_hash(&(self.metadata.as_slice(), t)); + self.extra_filename = format!("-{}", new_metadata); + self.metadata = new_metadata; + } +} + static central_repo: &'static str = "http://rust-lang.org/central-repo"; impl Show for PackageId { diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 40607e937..1f9f1ffb0 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -559,13 +559,18 @@ fn normalize(libs: &[TomlLibTarget], } fn test_targets(dst: &mut Vec, tests: &[TomlTestTarget], - default: |&TomlTestTarget| -> String) { + metadata: &Metadata, + default: |&TomlTestTarget| -> String) { for test in tests.iter() { let path = test.path.clone().unwrap_or_else(|| { TomlString(default(test)) }); let profile = &Profile::default_test(); + // make sure this metadata is different from any same-named libs. + let mut metadata = metadata.clone(); + metadata.mix(&format!("test-{}", test.name.as_slice())); + dst.push(Target::test_target(test.name.as_slice(), &path.to_path(), profile, diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 769c82381..910f0437f 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -42,6 +42,35 @@ test!(cargo_test_simple { assert_that(&p.bin("test/foo"), existing_file()); }) +test!(many_similar_names { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", " + pub fn foo() {} + #[test] fn lib_test() {} + ") + .file("src/main.rs", " + extern crate foo; + fn main() {} + #[test] fn bin_test() { foo::foo() } + ") + .file("tests/foo.rs", r#" + extern crate foo; + #[test] fn test_test() { foo::foo() } + "#); + + let output = p.cargo_process("cargo-test").exec_with_output().assert(); + let output = str::from_utf8(output.output.as_slice()).assert(); + assert!(output.contains("test bin_test"), "bin_test missing\n{}", output); + assert!(output.contains("test lib_test"), "lib_test missing\n{}", output); + assert!(output.contains("test test_test"), "test_test missing\n{}", output); +}) + test!(cargo_test_failing_test { let p = project("foo") .file("Cargo.toml", basic_bin_manifest("foo").as_slice()) -- 2.30.2